#!/usr/bin/env bash
#
# Run all go-iptables tests
#   ./test
#   ./test -v
#
# Run tests for one package
#   PKG=./unit ./test
#   PKG=ssh ./test
#
set -e

# Invoke ./cover for HTML output
COVER=${COVER:-"-cover"}

echo "Checking gofmt..."
fmtRes=$(gofmt -l -s .)
if [ -n "${fmtRes}" ]; then
	echo -e "gofmt checking failed:\n${fmtRes}"
	exit 255
fi

echo "Running tests..."
bin=$(mktemp)

go test -c -o ${bin} ${COVER} ./iptables/...
if [[ -z "$SUDO_PERMITTED" ]]; then
    echo "Test aborted for safety reasons. Please set the SUDO_PERMITTED variable."
    exit 1
fi

sudo -E bash -c "${bin} $@ ./iptables/..."
echo "Success"
rm "${bin}"
